2 * Adium is the legal property of its developers, whose names are listed in the copyright file included
3 * with this source distribution.
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11 * Public License for more details.
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 #import <Adium/AIMenuControllerProtocol.h>
18 #import <Adium/AIToolbarControllerProtocol.h>
19 #import "SHAutoValidatingTextView.h"
20 #import "SHLinkEditorWindowController.h"
21 #import "SHLinkManagementPlugin.h"
22 #import <AIUtilities/AIMenuAdditions.h>
23 #import <AIUtilities/AIStringAdditions.h>
24 #import <AIUtilities/AIToolbarUtilities.h>
25 #import <AIUtilities/AIImageAdditions.h>
26 #import <AIUtilities/AIWindowAdditions.h>
28 #define ADD_LINK_TITLE [AILocalizedString(@"Add Link",nil) stringByAppendingEllipsis]
29 #define EDIT_LINK_TITLE [AILocalizedString(@"Edit Link",nil) stringByAppendingEllipsis]
30 #define RM_LINK_TITLE AILocalizedString(@"Remove Link",nil)
32 @interface SHLinkManagementPlugin (PRIVATE)
33 - (BOOL)textViewSelectionIsLink:(NSTextView *)textView;
34 - (void)registerToolbarItem;
37 @implementation SHLinkManagementPlugin
43 //Add/Edit Link... menu item (edit menu)
44 menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:EDIT_LINK_TITLE
46 action:@selector(editFormattedLink:)
47 keyEquivalent:@"k"] autorelease];
48 [[adium menuController] addMenuItem:menuItem toLocation:LOC_Edit_Links];
51 menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:EDIT_LINK_TITLE
53 action:@selector(editFormattedLink:)
54 keyEquivalent:@""] autorelease];
55 [[adium menuController] addContextualMenuItem:menuItem toLocation:Context_TextView_LinkEditing];
56 [self registerToolbarItem];
59 menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:RM_LINK_TITLE
61 action:@selector(removeFormattedLink:)
62 keyEquivalent:@""] autorelease];
63 [[adium menuController] addContextualMenuItem:menuItem toLocation:Context_TextView_LinkEditing];
66 - (void)uninstallPlugin
71 //Update our add/edit link menu item
72 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
74 NSResponder *responder = [[[NSApplication sharedApplication] keyWindow] firstResponder];
75 if (responder && [responder isKindOfClass:[NSTextView class]]) {
76 if ([[menuItem title] isEqualToString:RM_LINK_TITLE]) {
77 // only make remove link menu item active if slection is a link.
78 return [self textViewSelectionIsLink:(NSTextView *)responder];
80 //Update the menu item's title to reflect the current action
81 [menuItem setTitle:([self textViewSelectionIsLink:(NSTextView *)responder] ? EDIT_LINK_TITLE : ADD_LINK_TITLE)];
83 return [(NSTextView *)responder isEditable];
86 return NO; //Disable the menu item if a text field is not key
92 - (IBAction)editFormattedLink:(id)sender
94 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
95 NSTextView *earliestTextView = (NSTextView *)[keyWindow earliestResponderOfClass:[NSTextView class]];
97 if (earliestTextView &&
98 ![[keyWindow windowController] isKindOfClass:[SHLinkEditorWindowController class]]) {
99 [SHLinkEditorWindowController showLinkEditorForTextView:earliestTextView
102 notifyingTarget:nil];
106 - (IBAction)removeFormattedLink:(id)sender
108 NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
109 NSTextView *earliestTextView = (NSTextView *)[keyWindow earliestResponderOfClass:[NSTextView class]];
111 if (earliestTextView) {
112 NSRange selectedRange = [earliestTextView selectedRange];
113 id selectedLink = nil;
115 if ([[earliestTextView textStorage] length] &&
116 selectedRange.location != NSNotFound &&
117 selectedRange.location != [[earliestTextView textStorage] length]) {
119 selectedLink = [[earliestTextView textStorage] attribute:NSLinkAttributeName
120 atIndex:selectedRange.location
121 effectiveRange:&selectedRange];
122 [[earliestTextView textStorage] removeAttribute:NSLinkAttributeName range:selectedRange];
127 //Returns YES if a link is under the selection of the passed text view
128 - (BOOL)textViewSelectionIsLink:(NSTextView *)textView
130 id selectedLink = nil;
132 if ([[textView textStorage] length] &&
133 [textView selectedRange].location != NSNotFound &&
134 [textView selectedRange].location != [[textView textStorage] length]) {
135 NSRange selectionRange = [textView selectedRange];
136 selectedLink = [[textView textStorage] attribute:NSLinkAttributeName
137 atIndex:selectionRange.location
138 effectiveRange:&selectionRange];
141 return selectedLink != nil;
144 #pragma mark Toolbar Item stuff
146 - (void)registerToolbarItem
148 //Unregister the existing toolbar item first
150 [[adium toolbarController] unregisterToolbarItem:toolbarItem forToolbarType:@"TextEntry"];
151 [toolbarItem release]; toolbarItem = nil;
154 toolbarItem = [[AIToolbarUtilities toolbarItemWithIdentifier:@"LinkEditor"
155 label:AILocalizedString(@"Link",nil)
156 paletteLabel:AILocalizedString(@"Insert Link",nil)
157 toolTip:AILocalizedString(@"Add/Edit Hyperlink",nil)
159 settingSelector:@selector(setImage:)
160 itemContent:[NSImage imageNamed:@"linkToolbar" forClass:[self class]]
161 action:@selector(editFormattedLink:)
164 [[adium toolbarController] registerToolbarItem:toolbarItem forToolbarType:@"TextEntry"];